package handler

import (
	"database-platform/handler/common"
	"database-platform/logger"
	"database-platform/model"
	"database-platform/service"
	"fmt"
	"github.com/gin-gonic/gin"
)

// ListAccountHandler 处理获取账号列表的请求
func ListAccountHandler(c *gin.Context) {
	params, err := getAccountListParams(c)
	if err != nil {
		msg := fmt.Sprintf("ListAccountHandler.error.message.%s", err.Error())
		logger.Logger.Error(msg)
		common.FailWithMsg(c, "参数解析错误")
		return
	}

	res, err := service.GetAccountList(params)
	if err != nil {
		common.FailWithMsg(c, err.Error())
		return
	}
	common.OkWithData(c, res)
}

// getAccountListParams 从请求中解析账号列表请求参数
func getAccountListParams(c *gin.Context) (params *model.AccountListRequest, err error) {
	params = &model.AccountListRequest{
		Username:     c.DefaultQuery("username", ""),
		UserType:     c.DefaultQuery("user_type", ""),
		ClusterID:    common.ParseInt(c.DefaultQuery("cluster_id", "")),
		DatabaseID:   common.ParseInt(c.DefaultQuery("database_id", "")),
		DBType:       c.DefaultQuery("db_type", ""),
		Search:       c.DefaultQuery("search", ""),
		ShowCluster:  common.ParseBool(c.DefaultQuery("show_cluster", "false")),
		ShowDatabase: common.ParseBool(c.DefaultQuery("show_database", "false")),
	}
	params.Page, params.PageSize = common.GetPagePageSizeParams(c)
	return params, err
}

// RetrieveAccountHandler 处理获取单个账号详情的请求
func RetrieveAccountHandler(c *gin.Context) {
	id, err := common.GetIdParams(c)
	if err != nil {
		logger.Logger.Error(fmt.Sprintf("RetrieveAccountHandler.error.message.%s", err.Error()))
		common.FailWithMsg(c, err.Error())
		return
	}
	result, err := service.GetAccountById(id)
	if err != nil {
		msg := fmt.Sprintf("RetrieveAccountHandler.error.message.%s", err.Error())
		logger.Logger.Error(msg)
		common.FailWithMsg(c, fmt.Sprintf("获取详情错误，id:%v, err:%s", id, err.Error()))
		return
	}
	common.OkWithData(c, result)
}

// InsertAccountHandler 处理插入账号记录的请求
func InsertAccountHandler(c *gin.Context) {
	var account model.Account
	if err := c.ShouldBindJSON(&account); err != nil {
		logger.Logger.Error(fmt.Sprintf("InsertAccountHandler.error.message.%s", err.Error()))
		common.FailWithMsg(c, "参数解析错误")
		return
	}

	err := service.InsertAccount(&account)
	if err != nil {
		msg := fmt.Sprintf("InsertAccountHandler.error.message.%s", err.Error())
		logger.Logger.Error(msg)
		common.FailWithMsg(c, err.Error())
		return
	}
	common.OkWithMsg(c, "插入成功")
}

// UpdateAccountHandler 处理更新账号记录的请求
func UpdateAccountHandler(c *gin.Context) {
	id, err := common.GetIdParams(c)
	if err != nil {
		logger.Logger.Error(fmt.Sprintf("UpdateAccountHandler.error.message.%s", err.Error()))
		common.FailWithMsg(c, err.Error())
		return
	}

	var account model.Account
	if err := c.ShouldBindJSON(&account); err != nil {
		logger.Logger.Error(fmt.Sprintf("UpdateAccountHandler.error.message.%s", err.Error()))
		common.FailWithMsg(c, "参数解析错误")
		return
	}

	err = service.UpdateAccount(id, &account)
	if err != nil {
		logger.Logger.Error(fmt.Sprintf("UpdateAccountHandler.error.message.%s", err.Error()))
		common.FailWithMsg(c, err.Error())
		return
	}
	common.OkWithMsg(c, "更新成功")
}

// DeleteAccountHandler 处理删除账号记录的请求
func DeleteAccountHandler(c *gin.Context) {
	id, err := common.GetIdParams(c)
	if err != nil {
		logger.Logger.Error(fmt.Sprintf("DeleteAccountHandler.error.message.%s", err.Error()))
		common.FailWithMsg(c, err.Error())
		return
	}

	err = service.DeleteAccount(id)
	if err != nil {
		msg := fmt.Sprintf("DeleteAccountHandler.error.message.%s", err.Error())
		logger.Logger.Error(msg)
		common.FailWithMsg(c, err.Error())
		return
	}
	common.OkWithMsg(c, "删除成功")
}
